import Vue from 'vue'
import VueRouter from 'vue-router'
import { getKey } from '../untils/token'
// @符号代表的是src文件夹。另外如果文件是index文件，可以不需要引入到index
// import Login from '@/views/login'
// import Layout from '@/views/layout'
// import Chart from '@/views/layout/chart'
// import Enterprise from '@/views/layout/enterprise'
// import Question from '@/views/layout/question'
// import Subject from '@/views/layout/subject'
// import User from '@/views/layout/user'
// import Welcome from '@/views/layout/welcome'

// 优化2：以下是路由懒加载的方法：
const Login = () => import('@/views/login')
const Layout = () => import('@/views/layout')
const Chart = () => import('@/views/layout/chart')
const Enterprise = () => import('@/views/layout/enterprise')
const Question = () => import('@/views/layout/question')
const Subject = () => import('@/views/layout/subject')
const User = () => import('@/views/layout/user')
const Welcome = () => import('@/views/layout/welcome')

// import parent from '@/views/test/parent'
// import father from '@/views/test/brother/father'
// import parent4 from '@/views/test/slot/parent'
// import parent3 from '@/views/test/nameslot/parent'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/',
    redirect: '/login'
  },
  // {
  //   path: '/parent',
  //   component: parent
  // },
  // {
  //   path: '/father',
  //   component: father
  // },
  // {
  //   path: '/slot',
  //   component: parent4
  // },
  // {
  //   path: '/nameslot',
  //   component: parent3
  // },
  {
    path: '/layout',
    name: 'Layout',
    component: Layout,
    children: [
      {
        path: 'welcome',
        component: Welcome,
        meta: {
          title: '欢迎登陆',
          icon: 'el-icon-loading',
          path: '/layout/welcome',
          rights: ['老师', '管理员', '超级管理员', '学生']
        }
      },
      {
        path: 'enterprise',
        component: Enterprise,
        meta: {
          title: '企业列表',
          icon: 'el-icon-office-building',
          path: '/layout/enterprise',
          rights: ['老师', '管理员', '超级管理员']
        }
      },
      {
        path: 'question',
        component: Question,
        meta: {
          title: '题库列表',
          icon: 'el-icon-edit-outline',
          path: '/layout/question',
          rights: ['老师', '管理员', '超级管理员']
        }
      },
      {
        path: 'chart',
        component: Chart,
        meta: {
          title: '数据预览',
          icon: 'el-icon-pie-chart',
          path: '/layout/chart',
          rights: ['老师', '管理员', '超级管理员']
        }
      },
      {
        path: 'subject',
        component: Subject,
        meta: {
          title: '学科列表',
          icon: 'el-icon-notebook-2',
          path: '/layout/subject',
          rights: ['老师', '学生', '管理员', '超级管理员']
        }
      },
      {
        path: 'user',
        component: User,
        meta: {
          title: '用户列表',
          icon: 'el-icon-user',
          path: '/layout/user',
          rights: ['老师', '管理员', '超级管理员']
        }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.fullPath === '/login') {
    next()
  } else {
    const key = getKey()
    if (key) {
      next()
      // 有权限
      // console.log('进去')
    } else {
      next('/login')
      // 没有权限
      // console.log('没进去了')
    }
  }
})

// 动态的设置标题,利用到了路由元信息中的内容
router.afterEach((to, from) => {
  // console.log(to)
  // console.log(from)
  // 这里是，如果有，就显示路由元信息中的标题，没有就用默认的黑马面面
  document.title = to.meta.title || '黑马面面'
})

export default router
